package com.keffectpanel;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.io.FileNotFoundException;

import javax.swing.JPanel;

import com.keffect.KEffect;

/**
 * This class is a panel that is associated with a graphic effect.
 * 
 * <p>
 * Last version: February 2, 2013
 * <p>
 * 
 * @author Matias Varela
 *         <p>
 *         Student of Licenciatura en Ciencias de la Computacion, at the
 *         Universidad Nacional del Sur (Argentina).
 *         <p>
 *         project site: http://code.google.com/p/keffect-project/
 * 
 */
@SuppressWarnings("serial")
public class KEffectPanel extends JPanel {

	// the graphic effect associated with this panel.
	protected KEffect effect;
	protected boolean effectEnabled;
	
	public KEffectPanel(){
		super();
		effectEnabled=true;
		effect=null;
	}

	
	/**
	 * Set the graphic effect.
	 * 
	 * @param effect
	 *            the effect that is going to be associated with this panel.
	 * 
	 */
	public void setEffect(KEffect effect) {
		// if there is another effect running, then it must be stopped.
		if (this.effect != null) this.effect.stop();

		if (effect != null) {
			this.effect = effect;
			this.effect.setEffectPanel(this);
		} else effect = null;
	}


	@Override
	public void paint(Graphics g) {
		if (this.effectEnabled) {
			Graphics2D g2 = (Graphics2D) g;

			//g2.setRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));

			if (effect != null && effect.isActive()) {
				effect.doPaintBefore(g2);
				super.paint(g2);
				effect.doPaintAfter(g2);
			} else super.paint(g2);

			g2.dispose();
		} else super.paint(g);
	}


	/**
	 * Start running the graphic effect. (The graphic effect will only start to
	 * run if it is enabled).
	 * 
	 * @throws FileNotFoundException
	 * 
	 * @throws NullEffectException
	 *             IF the graphic effect is not defined.
	 */
	public void startEffect() {
		if (this.effect == null) throw new NullPointerException("No graphic effect defined");
		if (this.effectEnabled) this.effect.start();
	}


	/**
	 * Returns whether the graphic effect is defined.
	 * 
	 * @return <code>true</code> if the graphic effect is defined;
	 *         <code>false</code> otherwise.
	 */
	public boolean hasEffect() {
		return this.effect != null;
	}


	/**
	 * Set whether the graphic effect is enabled. If <code>effectEnabled</code>
	 * is <code>false</code> then the graphic effect will never start to run;
	 * <code>true</code> it will.
	 * 
	 * @param effectEnabled
	 *            whether the graphic effect is enabled to run.
	 */
	public void setEffectEnabled(boolean effectEnabled) {
		this.effectEnabled = effectEnabled;
	}


	/**
	 * Returns whether the graphic effect is enabled to run.
	 * 
	 * @return whether the graphic effect is enabled to run.
	 */
	public boolean isEffectEnabled() {
		return this.effectEnabled;
	}


	/**
	 * Stop running the graphic effect.
	 */
	public void stopEffect() {
		if (this.effect != null) this.effect.stop();
		this.setEffect(null);
	}


	/**
	 * Returns whether the graphic effect is actually running.
	 * 
	 * @return <code>true</code> if the graphic effect is actually running;
	 *         <code>false</code> otherwise.
	 */
	public boolean isEffectRunning() {
		return this.effect != null && this.effect.isActive();
	}

}
